package vn.sunnet.game.SinhTon.screen;

import vn.sunnet.game.SinhTon.global.Assets;
import vn.sunnet.game.SinhTon.global.Setting;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

public class LoadingScreen extends BaseScreen {
	private float timeLoading;
	public static AssetManager manager, managerMusic;

	public LoadingScreen(MyGame game) {
		super(game);

		manager = new AssetManager();
		// manager = new AssetManager(game.request.getResolver()); // dung de
		// load
		// anh

		managerMusic = new AssetManager(); // dung de load nhac

		manager.load("TexturePacker/NhanVat.pack", TextureAtlas.class);

		manager.load("TexturePacker/SungDan.pack", TextureAtlas.class);

		manager.load("TexturePacker/skill.pack", TextureAtlas.class);

		manager.load("TexturePacker/locot.pack", TextureAtlas.class);

		manager.load("skin/button.atlas", TextureAtlas.class);
		manager.load("skin/button.json", Skin.class);

		manager.load("TexturePacker/boss.pack", TextureAtlas.class);

		loadMainMenu(); // load anh dung trong MenuScreen vao AssetManager
		loadSelectMap(); // load anh trong SelectMap vao AssetManager
		loadStage(); // load anh dung trong StageScreen vao AssetManager
		loadShop(); // load anh dung trong Shop vao AssetManager
		loadMap(); // load anh dung background map vao AssetManager
		loadZombie(); // load anh Zombie vao AssetManager
		loadPlayScreen(); // load anh dung trong SelectMap vao AssetManager

		loadMusicSound(); // load Music dung trong game vao AssetManager
		// Bat nhac trong man hinh loading
		if (Setting.isSound(Setting.MUSIC))
			Assets.mLoading.play();
		else
			Assets.mLoading.pause();
	}

	float loadGame;

	private void loadTexture(String file) {
		manager.load(file, Texture.class);
	}

	private void loadSound(String file) {
		managerMusic.load(file, Sound.class);
	}

	private void loadMusic(String file) {
		managerMusic.load(file, Music.class);
	}

	private void loadMusicSound() {
		// Sound
		loadSound("music/sBanVaoTuong.mp3");
		loadSound("music/sBiTanCong.mp3");
		loadSound("music/sClick.mp3");
		loadSound("music/sDoiSung.mp3");
		loadSound("music/sGameOver.mp3");
		loadSound("music/sGameWin.mp3");
		loadSound("music/sGun1.mp3");
		loadSound("music/sGun2.mp3");
		loadSound("music/sGun3.mp3");
		loadSound("music/sGun4.mp3");
		loadSound("music/sGun5.mp3");
		loadSound("music/sGun6.mp3");

		loadSound("music/sGunCanonNo.mp3");
		loadSound("music/sMuaLocot.mp3");
		loadSound("music/sMuaSkill.mp3");
		loadSound("music/sMuaSung.mp3");
		loadSound("music/sNapXu.mp3");

		loadSound("music/sPressSkillFreeze.mp3");
		loadSound("music/sNoSkillLuuDan.mp3");
		loadSound("music/sNoSkillRocket.mp3");
		loadSound("music/sSkillFreeze.mp3");
		loadSound("music/sSkillLuuDan.mp3");
		loadSound("music/sSkillRocket.mp3");

		loadSound("music/sSuDung.mp3");
		loadSound("music/sTrungZombie.mp3");

		loadSound("music/sZombie1.mp3");
		loadSound("music/sZombie2.mp3");
		loadSound("music/sZombie3.mp3");
		loadSound("music/sZombie4.mp3");
		loadSound("music/sZombie5.mp3");
		loadSound("music/sZombie6.mp3");

		// Music
		loadMusic("music/mMainMenu.mp3");
		loadMusic("music/mOptionShop.mp3");

		loadMusic("music/musicGame1.mp3");
		loadMusic("music/musicGame2.mp3");
		loadMusic("music/musicGame3.mp3");
		loadMusic("music/musicGame4.mp3");

		loadMusic("music/musicGapBoss.mp3");
	}

	private void loadMainMenu() {
		loadTexture("menu/bgMenu.png");
		loadTexture("menu/v1.png");
		loadTexture("menu/v2.png");
		loadTexture("menu/play.png");
		loadTexture("menu/play_hover.png");
	}

	private void loadZombie() {
		loadTexture("play/zombie/straight1.png");
		loadTexture("play/zombie/straight2.png");
		loadTexture("play/zombie/straight3.png");
		loadTexture("play/zombie/straight4.png");
		loadTexture("play/zombie/straight5.png");
		loadTexture("play/zombie/straight6.png");
		loadTexture("play/zombie/straight7.png");
		loadTexture("play/zombie/straight8.png");
		loadTexture("play/zombie/straight9.png");
		loadTexture("play/zombie/straight10.png");
		loadTexture("play/zombie/straight11.png");

		loadTexture("play/zombie/incline1.png");
		loadTexture("play/zombie/incline2.png");
		loadTexture("play/zombie/incline3.png");
		loadTexture("play/zombie/incline4.png");
		loadTexture("play/zombie/incline5.png");
		loadTexture("play/zombie/incline6.png");
		loadTexture("play/zombie/incline7.png");
		loadTexture("play/zombie/incline8.png");
		loadTexture("play/zombie/incline9.png");
		loadTexture("play/zombie/incline10.png");
		loadTexture("play/zombie/incline11.png");

		loadTexture("play/zombie/atIncline1.png");
		loadTexture("play/zombie/atIncline2.png");
		loadTexture("play/zombie/atIncline3.png");
		loadTexture("play/zombie/atIncline4.png");
		loadTexture("play/zombie/atIncline5.png");
		loadTexture("play/zombie/atIncline6.png");
		loadTexture("play/zombie/atIncline7.png");
		loadTexture("play/zombie/atIncline8.png");
		loadTexture("play/zombie/atIncline9.png");
		loadTexture("play/zombie/atIncline10.png");
		loadTexture("play/zombie/atIncline11.png");

		loadTexture("play/zombie/atStraight1.png");
		loadTexture("play/zombie/atStraight2.png");
		loadTexture("play/zombie/atStraight3.png");
		loadTexture("play/zombie/atStraight4.png");
		loadTexture("play/zombie/atStraight5.png");
		loadTexture("play/zombie/atStraight6.png");
		loadTexture("play/zombie/atStraight7.png");
		loadTexture("play/zombie/atStraight8.png");
		loadTexture("play/zombie/atStraight9.png");
		loadTexture("play/zombie/atStraight10.png");
		loadTexture("play/zombie/atStraight11.png");

		loadTexture("play/zombie/dead1.png");
		loadTexture("play/zombie/dead2.png");
		loadTexture("play/zombie/dead3.png");
	}

	private void loadSelectMap() {
		manager.load("TexturePacker/location.pack", TextureAtlas.class);
		manager.load("TexturePacker/map.pack", TextureAtlas.class);

		loadTexture("selectMap/bgSelectMap.png");
		loadTexture("selectMap/mapVN.png");
		loadTexture("selectMap/point.png");
		loadTexture("selectMap/point1.png");
	}

	private void loadStage() {
		manager.load("TexturePacker/lockStage.pack", TextureAtlas.class);
		loadTexture("stage/bgStage.png");
		loadTexture("stage/stageLock.png");
	}

	private void loadMap() {
		loadTexture("play/bgMap/bgMap1.png");
		loadTexture("play/bgMap/bgMap2.png");
		loadTexture("play/bgMap/bgMap3.png");
		loadTexture("play/bgMap/bgMap4.png");
		loadTexture("play/bgMap/bgMap5.png");
		loadTexture("play/bgMap/bgMap6.png");
		loadTexture("play/bgMap/bgMap7.png");
		loadTexture("play/bgMap/bgMap8.png");
		loadTexture("play/bgMap/bgMap9.png");
		loadTexture("play/bgMap/bgMap10.png");
	}

	private void loadPlayScreen() {
		manager.load("TexturePacker/image.pack", TextureAtlas.class);

		loadTexture("play/item/wave1.png");
		loadTexture("play/item/wave_end.png");

		loadTexture("play/item/plus.png");
		loadTexture("play/item/number.png");

		loadTexture("play/item/bgWin.png");

		loadTexture("play/skill/bgFreeze.png");
		loadTexture("play/skill/fire1.png");
		loadTexture("play/skill/fire2.png");
		loadTexture("play/skill/fire3.png");
	}

	private void loadShop() {
		manager.load("TexturePacker/GunSkill.pack", TextureAtlas.class);

		loadTexture("play/shop/bgShopGun.png");
		loadTexture("play/shop/bgShopSkill.png");
		loadTexture("play/shop/bar.png");
		loadTexture("play/shop/bought.png");
		loadTexture("play/shop/popup_not_enough_xu.png");
	}

	@Override
	public void render(float delta) {
		super.render(delta);

		if (manager.update() && managerMusic.update()) {
			if (!Assets.GET_BUTTON)
				Assets.getButton();
			game.menuScreen = new MenuScreen(game);
			game.setScreen(game.menuScreen);
			// game.setScreen(new PlayScreen(game));
		}

		loadGame = (manager.getProgress() * 100 + managerMusic.getProgress() * 100) / 2;

		timeLoading += Gdx.graphics.getDeltaTime();
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		batch.draw(Assets.ttBgLoading, 0, 0);
		// Ve cac not cham khi loading
		if (0.5 < timeLoading && timeLoading < 1) {
			batch.draw(Assets.ttDotLoading, 155, 22,
					Assets.ttDotLoading.getWidth(),
					Assets.ttDotLoading.getHeight());
		} else if (timeLoading < 1.5 && timeLoading > 1) {
			batch.draw(Assets.ttDotLoading, 155, 22,
					Assets.ttDotLoading.getWidth(),
					Assets.ttDotLoading.getHeight());
			batch.draw(Assets.ttDotLoading, 164, 22,
					Assets.ttDotLoading.getWidth(),
					Assets.ttDotLoading.getHeight());
		} else if (timeLoading > 1.5 && timeLoading < 2) {
			batch.draw(Assets.ttDotLoading, 155, 22,
					Assets.ttDotLoading.getWidth(),
					Assets.ttDotLoading.getHeight());
			batch.draw(Assets.ttDotLoading, 164, 22,
					Assets.ttDotLoading.getWidth(),
					Assets.ttDotLoading.getHeight());
			batch.draw(Assets.ttDotLoading, 173, 22,
					Assets.ttDotLoading.getWidth(),
					Assets.ttDotLoading.getHeight());
		} else if (timeLoading > 2)
			timeLoading = 0;

		// Thanh loading chay tu trai qua phai
		// batch.draw(Assets.ttLoadingTask, 60, 43, 680 * loadGame / 100,
		// Assets.ttLoadingTask.getHeight());
		batch.end();
	}

	@Override
	public void show() {
		super.show();
	}

	@Override
	public void dispose() {
		super.dispose();
		Assets.LOADING = false;
		if (Assets.mLoading != null)
			Assets.mLoading.dispose();
	}

}
